﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class CustomerInfoAdd : BasePage
    {
        public string sAttachment = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList<tbContractServiceClassEntity>(new tbContractServiceClassEntity().GetAll(), ddl服务项目, "ID", "ClassName");//绑定服务类别
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup!=" + PubEnums.RoleGroup.客户.ToString("D")), ddl负责人, "ID", "RealName");

                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }


                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                        btnSave.Visible = false;
                    }
                }

            }
        }


        private void DataShow(int troubleId)
        {
            //tbSaleCustomerEntity entity = new tbSaleCustomerEntity();
            //entity = entity.GetById(troubleId);

            //if (entity != null)
            //{
            //    ViewState["Entity"] = entity;

            //    txt公司名称.Text = entity.公司名称;
            //    ddl服务项目.SelectedValue = entity.服务项目;
            //    txt公司地址.Text = entity.公司地址;
            //    txt公司规模.Text = entity.公司规模;
            //    txt公司电话.Text = entity.公司电话;
            //    txt公司传真.Text = entity.公司传真;
            //    txt公司邮箱.Text = entity.公司邮箱;
            //    txt公司网站.Text = entity.公司网站;
            //    txt负责人1.Text = entity.负责人A;
            //    txt负责人2.Text = entity.负责人B;
            //    txt电话1.Text = entity.负责人A电话;
            //    txt电话2.Text = entity.负责人B电话;
            //    txt手机1.Text = entity.负责人A手机;
            //    txt手机2.Text = entity.负责人B手机;
            //    txt工作时间.Text = entity.工作时间.ToString();

            //    if (entity.五天八小时.Value)
            //    {
            //        CheckBox1.Checked = true;
            //    }
            //    if (entity.六天八小时.Value)
            //    {
            //        CheckBox2.Checked = true;
            //    }

                
            //    txt上班时间.Text = entity.上班时间.ToString();
            //    txt下班时间.Text = entity.下班时间.ToString();
            //    txt台试电脑.Text = entity.台式电脑;
            //    txt笔记本电脑.Text = entity.笔记本电脑;
            //    txt服务器.Text = entity.服务器;
            //    txt路由器.Text = entity.路由器;
            //    txt集线器.Text = entity.集线器;
            //    txtModen.Text = entity.Moden;
            //    txt交换机.Text = entity.交换机;
            //    txt电话交换.Text = entity.电话交换;
            //    txtVPN.Text = entity.VPN;
            //    txt邮件服务.Text = entity.邮件服务;
            //    txt网站服务.Text = entity.网站服务;
            //    txt打印机.Text = entity.打印机;
            //    txt复印机.Text = entity.复印机;
            //    txt扫描仪.Text = entity.扫描仪;
            //    txt其它要求.Text = entity.其他要求;
            //    txt五八小时服务.Text = entity.五八小时服务;
            //    txt应急响应次数.Text = entity.应急响应次数.ToString();
            //    txt备注.Text = entity.备注;
            //    ddl负责人.SelectedValue = entity.负责人.ToString();

            //    DataSet dsTemp = new tbAppendixEntity().GetDataSet(" select NewFileName from tb_Appendix where  BusinessID=" + entity.ID.ToString() + "");
            //    if (dsTemp.Tables[0].Rows.Count>0)
            //    {
            //        sAttachment = dsTemp.Tables[0].Rows[0]["NewFileName"].ToString();
            //        Label1.Visible = true;
            //    }              

            //    if (entity.日期时间.HasValue)
            //    {
            //        txt日期.Text = entity.日期时间.Value.ToShortDateString();
            //    }
            //}
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    Entity.tbSaleCustomerEntity entity = new ITOMMS.Entity.tbSaleCustomerEntity();
            //    if (ViewState["Entity"] != null)
            //    {
            //        entity = ViewState["Entity"] as tbSaleCustomerEntity;
            //    }
            //    else
            //    {
            //        entity.CreateBy = SessionHelper.UserID;
            //        entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //    }

            //    entity.公司名称 = txt公司名称.Text;
            //    entity.服务项目 = ddl服务项目.SelectedValue;
            //    entity.公司地址 = txt公司地址.Text;
            //    entity.公司规模 = txt公司规模.Text;
            //    entity.公司电话 = txt公司电话.Text;
            //    entity.公司传真 = txt公司传真.Text;
            //    entity.公司邮箱 = txt公司邮箱.Text;
            //    entity.公司网站 = txt公司网站.Text;
            //    entity.负责人A = txt负责人1.Text;
            //    entity.负责人B = txt负责人2.Text;
            //    entity.负责人A电话 = txt电话1.Text;
            //    entity.负责人B电话 = txt电话2.Text;
            //    entity.负责人A手机 = txt手机1.Text;
            //    entity.负责人B手机 = txt手机2.Text;
             

            //    if (CheckBox1.Checked)
            //    {
            //        entity.五天八小时 = true;
            //    }
            //    else
            //    {
            //        entity.五天八小时 = false;
            //    }
            //    if (CheckBox2.Checked)
            //    {
            //        entity.六天八小时 = true;
            //    }
            //    else
            //    {
            //        entity.六天八小时 = false;
            //    }


            //    entity.台式电脑 = txt台试电脑.Text;
            //    entity.笔记本电脑 = txt笔记本电脑.Text;
            //    entity.服务器 = txt服务器.Text;
            //    entity.路由器 = txt路由器.Text;
            //    entity.集线器 = txt集线器.Text;
            //    entity.Moden = txtModen.Text;
            //    entity.交换机 = txt交换机.Text;
            //    entity.电话交换 = txt电话交换.Text;
            //    entity.VPN = txtVPN.Text;
            //    entity.邮件服务 = txt邮件服务.Text;
            //    entity.网站服务 = txt网站服务.Text;
            //    entity.打印机 = txt打印机.Text;
            //    entity.复印机 = txt复印机.Text;
            //    entity.扫描仪 = txt扫描仪.Text;
            //    entity.其他要求 = txt其它要求.Text;
            //    entity.五八小时服务 = txt五八小时服务.Text;
            //    if (txt应急响应次数.Text.Trim() != string.Empty)
            //    {
            //        entity.应急响应次数 = int.Parse(txt应急响应次数.Text.ToString());
            //    }
            //    entity.备注 = txt备注.Text;
            //    entity.负责人 = int.Parse(ddl负责人.SelectedValue.ToString());



            //    DateTime workon = new DateTime();
            //    if (DateTime.TryParse(txt上班时间.Text.Trim(), out  workon))
            //    {
            //        entity.上班时间 = workon;
            //    }


            //    DateTime workoff = new DateTime();
            //    if (DateTime.TryParse(txt下班时间.Text.Trim(), out  workoff))
            //    {
            //        entity.下班时间 = workoff;
            //    }

            //    DateTime workTime = new DateTime();
            //    if (DateTime.TryParse(txt工作时间.Text.Trim(), out  workTime))
            //    {
            //        entity.工作时间 = workTime;
            //    }

            //    DateTime rs = new DateTime();
            //    if (DateTime.TryParse(txt日期.Text.Trim(), out  rs))
            //    {
            //        entity.日期时间 = rs;
            //    }
            //    entity.ModifyBy = SessionHelper.UserID;
            //    entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


            //    entity.SaveOrUpdate(entity);

            //    SaveImgToServer(FileUpload1, entity.ID);

            //    ViewState["Entity"] = entity;
            //    MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            //}
            //catch (Exception ex)
            //{
            //    log4netHelper.error(ex);
            //    MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            //}
        }


        private void SaveImgToServer(FileUpload imageUpload, int ID)
        {
            string rs = string.Empty;
            try
            {
                if (!imageUpload.HasFile)
                {
                    return;
                }
                //更新处理
                tbAppendixEntity entity = new tbAppendixEntity();
                entity = entity.FindEntity("BusinessID=" + ID);

                string year = System.DateTime.Now.Year.ToString();
                string month = System.DateTime.Now.Month.ToString();
                string day = System.DateTime.Now.Day.ToString();
                string hour = System.DateTime.Now.Hour.ToString();
                string minute = System.DateTime.Now.Minute.ToString();
                string second = System.DateTime.Now.Second.ToString();
                string fileName = year + month + day + hour + minute + second;

                string filePath = imageUpload.PostedFile.FileName;
                string fileEx = filePath.Substring(filePath.LastIndexOf(".") + 1).ToLower();
                if (!System.IO.Directory.Exists(Server.MapPath(@"~/UpLoadFiles\")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/UpLoadFiles\"));
                }


                //大小图
                string serverpath = Server.MapPath(@"~/UpLoadFiles\") + fileName + "." + fileEx;
                imageUpload.PostedFile.SaveAs(serverpath);

                rs = fileName + "." + fileEx;
                if (entity == null)
                {
                    entity = new tbAppendixEntity();
                }
                else
                {
                   // rs = entity.NewFileName;
                }
                entity.NewFileName = rs;
                entity.OriginalFileName = imageUpload.FileName;
                entity.Remark = "客户信息附件";
                entity.BusinessType = "客户信息";
                entity.BusinessID = ID;
                entity.SaveOrUpdate(entity);

            }
            catch
            {
                MessageBoxHelper.PopUpMessage("附件上传失败");
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerInfoList.aspx", true);
        }

      


    }
}
